package com.haiheng.library_base.utils;

import android.util.Log;

import com.haiheng.library.log.LogUtils;

import java.util.List;

/**
 * @Des:
 * @Author: hwz
 * @Date:2021/9/17 0017
 * @Version: 1.0
 */
public class FastClickUtil {
    private static long lastClickTime = 0;
    private static long firstTime  = 0;
    private static int lastButtonId = -1;
    private static long DIFF = 1000;    //时间间隔
    private static int fashtClicNum  = 0;
    private static int lastSize = 0;
    private static List<Object> lastObjectList = null;

    /**
     * 判断两次点击的间隔，如果小于1s，则认为是多次无效点击（任意两个view，固定时长1s）
     *
     * @return
     */
    public static boolean isFastDoubleClick() {
        return isFastDoubleClick(-1, DIFF);
    }

    /**
     * 判断两次点击的间隔，如果小于diff，则认为是多次无效点击（任意两个view，自定义间隔时长）
     *
     * @return
     */
    public static boolean isFastDoubleClick(long diff) {
        return isFastDoubleClick(-1, diff);
    }

    /**
     * 判断两次点击的间隔，如果小于1s，则认为是多次无效点击（同一个view，固定时长1s）
     *
     * @return
     */
    public static boolean isFastDoubleClick(int buttonId) {
        return isFastDoubleClick(buttonId, DIFF);
    }

    /**
     * 判断两次点击的间隔，如果小于diff，则认为是多次无效点击（同一按钮，自定义间隔时长）
     *
     * @param diff
     * @return
     */
    public static boolean isFastDoubleClick(int buttonId, long diff) {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (lastButtonId == buttonId && lastClickTime > 0 && timeD < diff) {
            Log.d("isFastDoubleClick", "短时间内view被多次点击");
            return true;
        }
        lastClickTime = time;
        lastButtonId = buttonId;
        return false;
    }

    public static boolean isFastFiveClick(int buttoId,long diff){
        long time = System.currentTimeMillis();
        long time1 = time - lastClickTime;
        if (time1 > diff){
            fashtClicNum = 0;
        }
        fashtClicNum = fashtClicNum+1;
        if (lastButtonId == buttoId && lastClickTime > 0 && time1 < diff  ){
            if (fashtClicNum == 5){
                fashtClicNum = 0;
                return true;
            }
        }
        lastClickTime = time;
        lastButtonId = buttoId;
        return false;
    }

    public static boolean isChange(List<Object> tagList,long diff){
        int size = tagList.size();
        long time = System.currentTimeMillis();
        long time1 = time - lastClickTime;
        if (time1 > diff){
            //时间超过间隔，则重置空上一次记录
            firstTime = 0;
            lastSize = -1;
            lastObjectList = null;
        }

        if (lastSize == size && firstTime == 0){
            //前面两次结果相同，记录时间
            firstTime  = time;
        }
        LogUtils.e("SIMPLE_LOGGER 长度是否改变lastSize："+lastSize+"__size:"+size+"__firstTime:"+firstTime);
        if (lastSize > -1 && lastSize != size ){
            //前两次盘点的结果不一样着直接返回
            lastSize=-1;
            firstTime = 0;
            lastObjectList = null;
            return true;
        }
        if (lastSize > -1 && lastSize == size){
            if (lastObjectList != null && !isListEqual(tagList,lastObjectList)){
                LogUtils.e("SIMPLE_LOGGER 长度相同内容不同则直接返回");
                lastSize=-1;
                firstTime = 0;
                lastObjectList = null;
                return true;
            }else{
                LogUtils.e("SIMPLE_LOGGER 长度相同");
            }
        }
        if (firstTime > 0 && time - firstTime > 3000){
            LogUtils.e("SIMPLE_LOGGER 3s内长度没改变则返回停止盘点");
            lastSize = -1;
            firstTime = 0;
            lastObjectList = null;
            return true;
        }
        lastSize = size;
        lastClickTime = time;
        return false;
    }

    /**
     * 比较两个List集合是否相等
     * <p>注：1. 如果一个List的引用为<code>null</code>，或者其包含的元素个数为0，那么该List在本逻辑处理中都算作空；
     * <p>2. 泛型参数E涉及到对象，所以需要确保正确实现了对应对象的<code>equal()</code>方法。
     * @param list1
     * @param list2
     * @return
     */
    public static <E>boolean isListEqual(List<E> list1, List<E> list2) {
        // 两个list引用相同（包括两者都为空指针的情况）
        if (list1 == list2) {
            return true;
        }

        // 两个list都为空（包括空指针、元素个数为0）
        if ((list1 == null && list2 != null && list2.size() == 0)
                || (list2 == null && list1 != null && list1.size() == 0)) {
            return true;
        }


        // 两个list元素个数不相同
        if (list1.size() != list2.size()) {
            return false;
        }

        // 两个list元素个数已经相同，再比较两者内容
        // 采用这种可以忽略list中的元素的顺序
        // 涉及到对象的比较是否相同时，确保实现了equals()方法
        if (!list1.containsAll(list2)) {
            return false;
        }

        return true;
    }


    public static boolean isFaskMsg(int size,long diff){
        long time = System.currentTimeMillis();
        long time1 = time - lastClickTime;
        if (time1 > diff){
            lastSize = -1;
        }
//        if (lastClickTime > 0 && time1 < diff && lastSize == size ){
        if ( lastSize == size ){
//        if (lastClickTime > 0 && time1 < diff){
//            if (lastClickTime > 0 && time1 < diff ){
//                return false;
//            }
            return true;
        }
        lastSize =size;
        lastClickTime = time;
        return false;
    }

}
